package com.tj.collection.db.dao.impl;

import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.google.common.base.Splitter;
import com.google.common.collect.Maps;
import com.tj.collection.db.dao.CusomExtDao;
import com.tj.collection.db.entity.RptCustomQuerySqlEntity;
import com.tj.collection.task.ChatsPaymentDataTask;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;

/**
 * @author linqu
 */
@Repository
public class CustomerExtDaoImpl extends AbstractBaseMapper implements CusomExtDao {
    private static Log logger = LogFactory.getLog(CustomerExtDaoImpl.class);

    @Override
    public String customQuery(String sql) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("sql", sql);
        final List<Map<String, Object>> records = this.getSqlSession().selectList(this.getStatement("customQuery"), sql);
        if (CollectionUtils.isEmpty(records)) {
            return null;
        }
        return JSONObject.toJSONString(records);
    }

    @Override
    public List<Map<String, Object>> customQuery2(String sql) {
        return this.getSqlSession().selectList(this.getStatement("customQuery"), sql);
    }


    /**
     * 封装更新语句
     */
    @Override
    public String buildUpdateSql(RptCustomQuerySqlEntity customQuerySql, Map<String, Object> item) {
        if (CollectionUtils.isEmpty(item)) {
            return null;
        }
        String updateSqlStr = customQuerySql.getUpdateSqlText();
        final JSONObject updateSqlJson = JSONObject.parseObject(updateSqlStr);
        String updateSql = updateSqlJson.getString("updateSql");
        final String updateColumns = updateSqlJson.getString("updateColumn");
        try {

            if (StringUtils.isNotEmpty(updateColumns)) {
                final List<String> columns = Splitter.on(',').splitToList(updateColumns);
                for (String column : columns) {
                    updateSql = updateSql.replace("##" + column + "##", item.get(column) == null ? "0" : item.get(column).toString() );
                }
            }
            final String condition = updateSqlJson.getString("condition");
            if (StringUtils.isNotEmpty(condition)) {
                final List<String> conditionColumns = Splitter.on(',').splitToList(condition);
                for (String conditionColumn : conditionColumns) {
                    updateSql = updateSql.replace("##" + conditionColumn + "##", item.get(conditionColumn) == null ? "NoData" : item.get(conditionColumn).toString());
                }
            }
            logger.info("buildUpdateSql:{}", updateSql);
            return updateSql;
        } catch (Exception e) {
            logger.error("buildSqlError:{}, customQuerySql:{}", updateSql, JSONObject.toJSONString(customQuerySql), e);
        }
        return null;
    }

    @Override
    public int updateCustomerSql(String sql) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("sql", sql);
        return this.getSqlSession().update(this.getStatement("updateCustomerSql"), params);
    }
}